package me.zhengjie.modules.system.rest

import me.zhengjie.logging.annotation.Log
import me.zhengjie.modules.system.domain.City
import me.zhengjie.modules.system.service.CityService
import me.zhengjie.modules.system.service.dto.CityQueryCriteria
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.servlet.http.HttpServletResponse
import org.springframework.data.domain.Pageable
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import java.io.IOException

/**
 * @author kuki
 * @date 2022-07-21
 */
@Schema(description = "City管理")
@RestController
@RequestMapping("/api/city")
class CityController(private val cityService: CityService) {
    @Log("导出数据")
    @Schema(description = "导出数据")
    @GetMapping(value = ["/download"])
    @PreAuthorize("@el.check('city:list')")
    @Throws(
        IOException::class
    )
    fun download(response: HttpServletResponse?, criteria: CityQueryCriteria?) {
        cityService.download(cityService.queryAll(criteria!!), response)
    }

    @GetMapping
    @Log("查询City")
    @Schema(description = "查询City")
    @PreAuthorize("@el.check('city:list')")
    fun getCitys(criteria: CityQueryCriteria?, pageable: Pageable?): ResponseEntity<*> {
        return ResponseEntity(cityService.queryAll(criteria!!, pageable), HttpStatus.OK)
    }

    @PostMapping
    @Log("新增City")
    @Schema(description = "新增City")
    @PreAuthorize("@el.check('city:add')")
    fun create(@Validated @RequestBody resources: City?): ResponseEntity<*> {
        return ResponseEntity(cityService.create(resources!!), HttpStatus.CREATED)
    }

    @PutMapping
    @Log("修改City")
    @Schema(description = "修改City")
    @PreAuthorize("@el.check('city:edit')")
    fun update(@Validated @RequestBody resources: City?): ResponseEntity<*> {
        cityService.update(resources!!)
        return ResponseEntity<Any?>(HttpStatus.NO_CONTENT)
    }

    @DeleteMapping(value = ["/{id}"])
    @Log("删除City")
    @Schema(description = "删除City")
    @PreAuthorize("@el.check('city:del')")
    fun delete(@PathVariable id: Int?): ResponseEntity<*> {
        cityService.delete(id!!)
        return ResponseEntity<Any?>(HttpStatus.OK)
    }
}